//@version=5
indicator(title='Hamiseverim Binary Options (Safety Signals)', shorttitle='BO (Safety Signals)', overlay=true)
startCalc = 0.5
incrementCalc = 0.5
maximumCalc = 5.0
out = ta.sar(startCalc, incrementCalc, maximumCalc)
sarUp = ta.sar(startCalc, incrementCalc, maximumCalc)
sarDown = ta.sar(startCalc, incrementCalc, maximumCalc)

colUp = close >= sarDown ? #013220 : na
colDown = close <= sarUp ? #8a0303 : na

psar = ta.sar(startCalc, incrementCalc, maximumCalc)

src = close[0]
zero = 0.00

lights = input.string(title='Barcolor I / 0 ? ', options=['ON', 'OFF'], defval='OFF')

// Definition : BLOCKCHAIN PARAMETERS


// AVERAGE BLOCK SIZE

block = 'QUANDL:BCHAIN/AVBLS'

_block = request.security(block, 'D', close[0])

// API BLOCKCHAIN SIZE

bsize = 'QUANDL:BCHAIN/BLCHS'

_bsize = request.security(bsize, 'D', close[0])

// MINERS REVENUE

minrev = 'QUANDL:BCHAIN/MIREV'

_minrev = request.security(minrev, 'D', close[0])

// HASH RATE 

hrate = 'QUANDL:BCHAIN/HRATE'

_hrate = request.security(hrate, 'D', close[0])

// BITCOIN COST PER TRANSACTION 

cptra = 'QUANDL:BCHAIN/CPTRA'

_cptra = request.security(cptra, 'D', close[0])

// BITCOIN USD EXCHANGE TRADE VOLUME 

trvou = 'QUANDL:BCHAIN/TRVOU'

_trvou = request.security(trvou, 'D', close[0])

// BITCOIN TOTAL NUMBER OF TRANSACTIONS 

ntrat = 'QUANDL:BCHAIN/NTRAT'

_ntrat = request.security(ntrat, 'D', close[0])

// CHANGE 

// DEEP LEARNING INDICATORS


_indicator1 = (_block - _block[1]) / _block[1]
_indicator2 = (_bsize - _bsize[1]) / _bsize[1]
_indicator3 = (_minrev - _minrev[1]) / _minrev[1]
_indicator4 = (_hrate - _hrate[1]) / _hrate[1]
_indicator5 = (_cptra - _cptra[1]) / _cptra[1]
_indicator6 = (_trvou - _trvou[1]) / _trvou[1]
_indicator7 = (_ntrat - _ntrat[1]) / _ntrat[1]
src_chg = (src - src[1]) / src[1]


// Inputs on Tangent Function : 

tangentdiff(_src) =>
    nz((_src - _src[1]) / _src[1])


// Deep Learning Activation Function (Tanh) : 

ActivationFunctionTanh(v) =>
    (1 - math.exp(-2 * v)) / (1 + math.exp(-2 * v))


// DEEP LEARNING 

// INPUTS : 

input_1 = tangentdiff(_indicator1)
input_2 = tangentdiff(_indicator2)
input_3 = tangentdiff(_indicator3)
input_4 = tangentdiff(_indicator4)
input_5 = tangentdiff(_indicator5)
input_6 = tangentdiff(_indicator6)
input_7 = tangentdiff(_indicator7)


// LAYERS : 

// Input Layers 

n_0 = ActivationFunctionTanh(input_1 + 0)
n_1 = ActivationFunctionTanh(input_2 + 0)
n_2 = ActivationFunctionTanh(input_3 + 0)
n_3 = ActivationFunctionTanh(input_4 + 0)
n_4 = ActivationFunctionTanh(input_5 + 0)
n_5 = ActivationFunctionTanh(input_6 + 0)
n_6 = ActivationFunctionTanh(input_7 + 0)

// Hidden Layer 1 Node : 

n_7 = ActivationFunctionTanh(-4.781763 * n_0 + 10.461760 * n_1 + 3.315506 * n_2 + -2.904398 * n_3 + -3.395257 * n_4 + 1.198382 * n_5 + -0.673757 * n_6 + -4.189406)

n_8 = ActivationFunctionTanh(13.037002 * n_0 + -23.995687 * n_1 + -7.415491 * n_2 + 6.033804 * n_3 + 1.473729 * n_4 + -1.364151 * n_5 + 1.505107 * n_6 + 7.924747)

n_9 = ActivationFunctionTanh(6.872487 * n_0 + -8.574429 * n_1 + 3.611399 * n_2 + 0.685256 * n_3 + 6.047363 * n_4 + -2.365322 * n_5 + -8.248883 * n_6 + -2.230143)

n_10 = ActivationFunctionTanh(8.357794 * n_0 + 18.730190 * n_1 + 45.193578 * n_2 + -19.113609 * n_3 + 22.992774 * n_4 + -43.047078 * n_5 + -53.282232 * n_6 + -3.277878)

n_11 = ActivationFunctionTanh(-0.439029 * n_0 + 49.038087 * n_1 + 72.164782 * n_2 + -43.762872 * n_3 + 23.856610 * n_4 + -73.080145 * n_5 + -56.878190 * n_6 + -6.168150)

// OUTPUT

_output = ActivationFunctionTanh(-19.795264 * n_7 + -19.868848 * n_8 + -2.712585 * n_9 + 4.355774 * n_10 + -3.682000 * n_11 + 19.939365)


// Plot data 

_chg_src = tangentdiff(src) * 100

_seed = _output - _chg_src


// MACD : SEED 

fastLength = 12
slowlength = 26
signalLength = 9


macd = ta.ema(_seed, fastLength) - ta.ema(_seed, slowlength)
signal = ta.ema(macd, signalLength)


hist = macd - signal

//---------------------------------------------------------------------------------------------------------//
//Range&Trend Filter

maType = input.string('EMA', 'MA Type', options=['SMA', 'EMA', 'HMA', 'RMA'])
ma(t, s, l) =>
    maType == 'SMA' ? ta.sma(s, l) : maType == 'EMA' ? ta.ema(s, l) : maType == 'HMA' ? ta.hma(s, l) : maType == 'RMA' ? ta.rma(s, l) : na

maPeriod = input.int(10, 'MA Period', minval=1)
trendType = input.string('Trailing', 'Trend Type', options=['Flexible', 'Trailing'])

showRange = input(true, 'Show Ranges')
extend = input(false, 'Extend Line')

atrPeriod = input.int(100, 'ATR Period', minval=1)
atrMulti = input.float(1, 'ATR Multiplier', minval=0.1, step=0.1)

// —————————————————————————————————————————————————————————————————————————————
// —————————— SuperTrend

maHigh = ma(maType, high, maPeriod)
maLow = ma(maType, low, maPeriod)

atr = ta.atr(atrPeriod) * atrMulti

minLow = maLow - atr
float upTrend = na
upTrend := trendType == 'Flexible' ? minLow : close[1] > upTrend[1] ? math.max(minLow, upTrend[1]) : minLow
minHigh = maHigh - atr
float upRange = na
upRange := trendType == 'Flexible' ? minHigh : close[1] > upRange[1] ? math.max(minHigh, upRange[1]) : minHigh

plusHigh = maHigh + atr
float downTrend = na
downTrend := trendType == 'Flexible' ? plusHigh : close[1] < downTrend[1] ? math.min(plusHigh, downTrend[1]) : plusHigh
plusLow = maLow + atr
float downRange = na
downRange := trendType == 'Flexible' ? plusLow : close[1] < downRange[1] ? math.min(plusLow, downRange[1]) : plusLow

var up = 1
up := up != 1 and close > (showRange ? downRange : downTrend) ? 1 : up == 1 and close < upTrend ? -1 : up

var down = 1
down := down != 1 and close < (showRange ? upRange : upTrend) ? 1 : down == 1 and close > downTrend ? -1 : down

// —————————————————————————————————————————————————————————————————————————————
// —————————— Plot

plotSource = plot(close, editable=false, display=display.none)

startUp = up != up[1] and up == 1
plotUp = plot(up == 1 ? upTrend : na, 'Up', color.new(#013220, 0), 2, plot.style_linebr)
plotshape(startUp ? upTrend : na, 'Start Up', shape.circle, location.absolute, color.new(#013220, 0), size=size.tiny)
fill(plotSource, plotUp, color.new(color.navy, 90), 'Up')
alertcondition(startUp, 'Start Up', 'Start Up {{ticker}}')

startDown = down != down[1] and down == 1
plotDown = plot(down == 1 ? downTrend : na, 'Down', color.red, 2, plot.style_linebr)
plotshape(startDown ? downTrend : na, 'Start Down', shape.circle, location.absolute, color.red, size=size.tiny)
fill(plotSource, plotDown, color.new(color.navy, 90), 'Down')
alertcondition(startDown, 'Start Down', 'Start Down {{ticker}}')

upLine = if extend and up == 1
    line.new(bar_index[1], upTrend, bar_index, upTrend, xloc.bar_index, extend.right, color.new(#013220, 0), line.style_solid, 2)
line.delete(upLine[1])

downLine = if extend and down == 1
    line.new(bar_index[1], downTrend, bar_index, downTrend, xloc.bar_index, extend.right, color.red, line.style_solid, 2)
line.delete(downLine[1])
//----------------------//


lengthzx = input(50)
srczx = input(close)
//----
azx = 0.
bzx = 0.
azx := math.max(srczx, nz(azx[1])) - nz(azx[1] - bzx[1]) / lengthzx
bzx := math.min(srczx, nz(bzx[1])) + nz(azx[1] - bzx[1]) / lengthzx
avgzx = math.avg(azx, bzx)
//----
crossup = bzx[1] < close[1] and bzx > close
crossdn = azx[1] < close[1] and azx > close
bullish = ta.barssince(crossdn) <= ta.barssince(crossup)
czx = bullish ? color.lime : color.red

//--------------------//
// Next candle//
periodKcc = input.int(5, title='%K', minval=1)
smoothKcc = input.int(1, title='Smooth', minval=1)
kcc = ta.sma(ta.stoch(close, high, low, periodKcc), smoothKcc)

srccc = input(close, title='RSI Source')
lengthRSIcc = input.int(9, title='RSI Length', minval=1)
rsicc = ta.rsi(srccc, lengthRSIcc)


/////////////////////////////////////
/// Params
shape_loc = location.belowbar
shape_style = shape.labelup

////////////////////////////////////
/// Patterns
// Inside bars
ibar_0 = high < high[1] and low > low[1] and bullish
ibar_1 = high < high[2] and low > low[2] and high[1] < high[2] and low[1] > low[2] and bullish
ibar_2 = high < high[3] and low > low[3] and high[1] < high[3] and low[1] > low[3] and high[2] < high[3] and low[2] > low[3] and bullish

// Outside bars
obar_0 = high > high[1] and low < low[1] and bullish
obar_1 = high > high[2] and low < low[2] and high[1] > high[2] and low[1] < low[2] and bullish
obar_2 = high > high[3] and low < low[3] and high[1] > high[3] and low[1] < low[3] and high[2] > high[3] and low[2] < low[3] and bullish

////////////////////////////////////
/// Plot patterns
plotshape(ibar_0, title='Inside_bar', location=shape_loc, color=color.new(#013220, 0), style=shape.labelup, textcolor=color.new(#ffffff, 0), text='Call')

//--------------------//

// Conditions : 

positive_condition = hist < 0
negative_condition = hist > 0

neg_moment = ta.crossover(macd, signal)
pos_moment = ta.crossunder(macd, signal)

bullishPSAR = psar < high and psar[1] > low and hist < 0 and upTrend and bullish and kcc > rsicc
bearishPSAR = psar > low and psar[1] < high and hist > 0 and downTrend and not bullish and rsicc > kcc

plotshape(bullishPSAR, color=color.new(#013220, 0), style=shape.labelup, textcolor=color.new(#ffffff, 0), text='Call', location=location.belowbar)
plotshape(bearishPSAR, color=color.new(#8a0303, 0), style=shape.labeldown, textcolor=color.new(#ffffff, 0), text='Put', location=location.abovebar)

alertcondition(bullishPSAR, title='Buy SAR', message='Buy SAR')
alertcondition(bearishPSAR, title='Sell SAR', message='Sell SAR')

